package com.example.demoorder.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demoorder.bizservice.TestBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
public class TestCtl {
    /**
     * log
     */
    Logger logger = LoggerFactory.getLogger(TestCtl.class);
    @Resource
    private TestBizService testBizService;

    @RequestMapping("/test")
    public Object get() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Map result = testBizService.restFegin();
        stopWatch.stop();
        logger.info(stopWatch.prettyPrint());
        return result;
    }

    @PostMapping("/testPost")
    public Object testPost(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject.toJSONString());
        jsonObject.put("sign", UUID.randomUUID().toString().replace("-", ""));
        return jsonObject;
    }

    @PostMapping("/testUpload")
    public Object testUpload(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "D://testupload//"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";

    }
}
